package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.entity.CalculateSportgoalEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.model.enums.ActivityCompat;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.widget.dialog.SetTargetDialog;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class SetTargetActivity extends BaseActivity {
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.tv_1)
    TextView tv_1;
    @BindView(R.id.tv_2)
    TextView tv_2;
    @BindView(R.id.tv_3)
    TextView tv_3;
    @BindView(R.id.tv_4)
    TextView tv_4;
    @BindView(R.id.tv_count)
    TextView tv_count;
    @BindView(R.id.tv_danwei)
    TextView tv_danwei;
    @BindView(R.id.tv_start_end_name)
    TextView tv_start_end_name;
    @BindView(R.id.tv_progress)
    TextView tv_progress;
    @BindView(R.id.tv_days)
    TextView tv_days;
    @BindView(R.id.tv_count_desc)
    TextView tv_count_desc;

    private int type = 1;//1:步数；2：健走；3：跑步；4：骑行
    private String sectionId;
    private String startEndName;
    private String count;
    private String days;
    private double progress;

    private int number1 = 5000;
    private int number2 = 3;
    private int number3 = 5;
    private int number4 = 10;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_target;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        top_title.setText("设置运动目标");
        type = getIntent().getIntExtra(ActivityCompat.TYPE, 1);
        sectionId = getIntent().getStringExtra(ActivityCompat.SECTION_ID);
        progress = getIntent().getDoubleExtra(ActivityCompat.PROGRESS, 0);
        startEndName = getIntent().getStringExtra(ActivityCompat.START_END_NAME);
        count = getIntent().getStringExtra(ActivityCompat.COUNT);
        if (TextUtils.isEmpty(count)) {
            count = "0";
        }
        days = getIntent().getStringExtra(ActivityCompat.DAYS);
        if (type == 1) {
            number1 = Integer.parseInt(count);
        } else if (type == 2)
            number2 = Integer.parseInt(count);
        else if (type == 3)
            number3 = Integer.parseInt(count);
        else
            number4 = Integer.parseInt(count);
        setData();
        setType(type);
    }

    private void initData(String sectionId, double progress, String number, int type) {
        showDialog();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("sectionId", sectionId);
            jsonObject.put("sectionProgress", 1 - progress);
            jsonObject.put("number", number);
            jsonObject.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new ChallengeAPI().calculateSportGoal(jsonObject.toString(), new BaseResultCallback<CalculateSportgoalEntity>() {
            @Override
            public void onResponse(CalculateSportgoalEntity response) {
                dismissDialog();
                if (response != null && response.code == 0) {
                    count = response.data.number + "";
                    days = response.data.day + "";
                    setData();
                    EventBus.getDefault().post(new EventBusEntity(EventBusEntity.CODE_108));
                }
            }
        });
    }

    private void setData() {
        tv_progress.setText("目前进度：" + CommonUtil.Double2position(progress * 100) + "%");
        tv_start_end_name.setText(startEndName);
        tv_count.setText(count);
        String days = "目标设置后，大约需要 %s 天完成此路段";
        tv_days.setText(String.format(days, this.days));
    }

    private void setType(int type) {
        this.type = type;
        tv_1.setBackgroundResource(R.drawable.bg_f6f5f8_radius_6dp);
        tv_2.setBackgroundResource(R.drawable.bg_f6f5f8_radius_6dp);
        tv_3.setBackgroundResource(R.drawable.bg_f6f5f8_radius_6dp);
        tv_4.setBackgroundResource(R.drawable.bg_f6f5f8_radius_6dp);

        tv_1.setTextColor(getResources().getColor(R.color.color_FF999999));
        tv_2.setTextColor(getResources().getColor(R.color.color_FF999999));
        tv_3.setTextColor(getResources().getColor(R.color.color_FF999999));
        tv_4.setTextColor(getResources().getColor(R.color.color_FF999999));
        switch (type) {
            case 1:
                tv_1.setBackgroundResource(R.drawable.bg_ee4238_df2526_6dp);
                tv_1.setTextColor(getResources().getColor(R.color.white));
                tv_danwei.setText("步");
                tv_count.setText(number1 + "");
                tv_count_desc.setText("每日步数目标（步数）");
                initData(sectionId, progress, number1 + "", type);
                break;
            case 2:
                tv_2.setBackgroundResource(R.drawable.bg_ee4238_df2526_6dp);
                tv_2.setTextColor(getResources().getColor(R.color.white));
                tv_danwei.setText("km");
                tv_count.setText(number2 + "");
                tv_count_desc.setText("每日健走目标（km）");
                initData(sectionId, progress, number2 + "", type);
                break;
            case 3:
                tv_3.setBackgroundResource(R.drawable.bg_ee4238_df2526_6dp);
                tv_3.setTextColor(getResources().getColor(R.color.white));
                tv_danwei.setText("km");
                tv_count.setText(number3 + "");
                tv_count_desc.setText("每日跑步目标（km）");
                initData(sectionId, progress, number3 + "", type);
                break;
            case 4:
                tv_4.setBackgroundResource(R.drawable.bg_ee4238_df2526_6dp);
                tv_4.setTextColor(getResources().getColor(R.color.white));
                tv_danwei.setText("km");
                tv_count.setText(number4 + "");
                tv_count_desc.setText("每日骑行目标（km）");
                initData(sectionId, progress, number4 + "", type);
                break;
        }
    }

    @OnClick({R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4, R.id.tv_count, R.id.top_btn_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.tv_1:
                setType(1);
                break;
            case R.id.tv_2:
                setType(2);
                break;
            case R.id.tv_3:
                setType(3);
                break;
            case R.id.tv_4:
                setType(4);
                break;
            case R.id.tv_count:
                SetTargetDialog dialog = new SetTargetDialog(SetTargetActivity.this, type, Integer.valueOf(tv_count.getText().toString())).builder();
                dialog.setOnSelect(new SetTargetDialog.OnSelect() {
                    @Override
                    public void onSelect(String str) {
                        tv_count.setText(str);
                        switch (type) {
                            case 1:
                                number1 = Integer.parseInt(str);
                                break;
                            case 2:
                                number2 = Integer.parseInt(str);
                                break;
                            case 3:
                                number3 = Integer.parseInt(str);
                                break;
                            case 4:
                                number4 = Integer.parseInt(str);
                                break;
                        }
                        initData(sectionId, progress, tv_count.getText().toString(), type);
                    }
                });
                dialog.show();
                break;
        }
    }
}
